class TeamsController < ApplicationController
  before_filter :login_required, :authorization_required, :find_customer

  verify :method => :put, :only => [ :update ], :redirect_to => { :action => :index }
  verify :method => :post, :only => [ :create ], :redirect_to => { :action => :index }
  verify :method => :delete, :only => [ :destroy ], :redirect_to => { :action => :index }
    
  def index
    if params[:query].nil?
      @teams = Team.find(:all, :order => 'name asc')
    else
      @teams = Team.find(:all, :conditions => ["name like ?", "%#{params[:query]}%"], :order => 'name asc')
    end
    respond_to do |format|
      format.html {render :action => 'list.html.erb'}
      format.js {render :action => 'list.js.rjs'}
      format.xml {render :xml => @teams.to_xml}
    end
  end

  def new
    @team = Team.new
    respond_to do |format|
      format.html {render :action => 'new.html.erb'}
      format.js {render :action => 'new.js.rjs'}
    end
  end
  
  def create
    @team = Team.new(params[:team])
    if @team.save
      index
    else
      render :action => 'new' unless request.xhr?
    end
  end
  
  def edit
    @team = Team.find(params[:id])
    respond_to do |format|
      format.html {render :action => 'edit.html.erb'}
      format.js {render :action => 'edit.js.rjs'}
    end
  end

  def update
    @team = Team.find(params[:id])
    if @team.update_attributes(params[:team])
      index
    else
      render :action => 'edit' unless request.xhr?
    end
  end

  def show
    @team = Team.find(params[:id])
  end
  
  def destroy_selected
    unless params[:selected].nil?
      params[:selected].each { |id| Team.find(id).destroy }
    end
    respond_to do |format|
      format.html {redirect_to :action => 'list'}
      format.js do
        @teams = Team.find(:all, :order => 'name')
        render :action => 'list.js.rjs'
      end
    end
  end
end
